Imports Microsoft.Win32
Imports System.IO

Public Class clsOfficeCheck
    Public Enum OfficeComponent
        Word
        Excel
        PowerPoint
        Outlook
    End Enum
    Public Enum OfficeVersion
        v2003
        v2007
    End Enum
    Public Function GetComponentPath(ByVal _component As OfficeComponent) As String
        Const RegKey As String = "Software\Microsoft\Windows\CurrentVersion\App Paths"
        Dim toReturn As String = String.Empty
        Dim _key As String = String.Empty

        Select Case _component
            Case OfficeComponent.Word
                _key = "winword.exe"
            Case OfficeComponent.Excel
                _key = "excel.exe"

            Case OfficeComponent.PowerPoint
                _key = "powerpnt.exe"


            Case OfficeComponent.Outlook
                _key = "outlook.exe"

        End Select

        'looks inside CURRENT_USER:
        Dim _mainKey As RegistryKey = Registry.CurrentUser
        Try
            _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, False)
            If Not _mainKey Is Nothing Then
                toReturn = _mainKey.GetValue(String.Empty).ToString()
            End If
        Catch ex As Exception
            Err.Clear()
        End Try


        'if not found, looks inside LOCAL_MACHINE:
        _mainKey = Registry.LocalMachine
        If String.IsNullOrEmpty(toReturn) Then
            Try
                _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, False)
                If Not _mainKey Is Nothing Then
                    toReturn = _mainKey.GetValue(String.Empty).ToString()
                End If
            Catch ex As Exception
                Err.Clear()
            End Try
        End If

        'closing the handle:
        If Not _mainKey Is Nothing Then
            _mainKey.Close()
        End If
        Return GetMajorVersion(toReturn)
        '' Return toReturn
    End Function
    Private Function GetMajorVersion(ByVal _path As String) As Integer
        Dim toReturn As Integer = 0
        If File.Exists(_path) Then
            Try
                Dim _fileVersion As FileVersionInfo = FileVersionInfo.GetVersionInfo(_path)
                toReturn = _fileVersion.FileMajorPart
                
            Catch ex As Exception
                Err.Clear()
            End Try
        End If
        Select Case toReturn
            Case 11
                Return "2003"
            Case 12
                Return "2007"
            Case Else
                Return "0"
        End Select
    End Function

End Class
